Imports DevExpress.XtraEditors.Repository

Public Class DialogCariSiswa
    Public VarNIS As String
    Public VarNAMA As String

    Private Sub txtCari_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCari.TextChanged
        Dim strQuery As String()
        Dim err As String = ""
        Dim dtsiswa As New DataTable
        GridControl1.DataSource = Nothing
        If cbTipeCari.Text = "NIS" Then
            strQuery = New String() {"*", "datasiswa", "nis", txtCari.Text}
            If Utils.executeSP("proc_searchallEqual", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()

                End If
            End If
        ElseIf cbTipeCari.Text = "Nama" Then
            strQuery = New String() {"*", "datasiswa", "nama", txtCari.Text}
            If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                End If
            End If
        End If
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim strQuery As String()
        Dim err As String = ""
        Dim dtsiswa As New DataTable
        GridControl1.DataSource = Nothing
        If cbTipeCari.Text = "NIS" Then
            strQuery = New String() {"*", "datasiswa", "nis", txtCari.Text}
            If Utils.executeSP("proc_searchallEqual", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                Else
                    MsgBox("Maaf Data Siswa Dengan " & cbTipeCari.Text & " = " & txtCari.Text & _
                           " Tidak Terdaftar", MsgBoxStyle.Exclamation, "Cari Data Siswa")
                End If
            End If
        ElseIf cbTipeCari.Text = "Nama" Then
            strQuery = New String() {"*", "datasiswa", "nama", txtCari.Text}
            If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                Else
                    MsgBox("Maaf Data Siswa Dengan " & cbTipeCari.Text & " = " & txtCari.Text & _
                           " Tidak Terdaftar", MsgBoxStyle.Exclamation, "Cari Data Siswa")
                End If
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If GridView1.RowCount > 0 Then
            Dim dr As DataRow
            dr = GridView1.GetDataRow(GridView1.FocusedRowHandle)
            VarNIS = IIf(IsDBNull(dr("nis")), "", dr("nis").ToString)
            VarNAMA = IIf(IsDBNull(dr("nama")), "", dr("nama").ToString)
        End If
        If LocalSession.SessForm = "TesKesehatan" Then
            Dim dtkesehatansiswa As New DataTable
            LoadDataKesehatanSiswa(dtkesehatansiswa)
            If dtkesehatansiswa.Rows.Count > 0 Then
                DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            Else
                MsgBox("Data Kesehatan Siswa Belum Tersedia", MsgBoxStyle.Information, "Informasi")
                Exit Sub
            End If
        Else
            DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
        
    End Sub
    Private Sub LoadDataSiswa(ByRef dtsiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "datasiswa", "nis", ""}
        If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
            If Utils.RowExist(dtsiswa) Then
                GridControl1.DataSource = dtsiswa
                GridControl1.Refresh()
                GridControl1.RefreshDataSource()

                GridView1.RefreshData()
            End If
        End If
    End Sub

    Private Sub LoadDataKesehatanSiswa(ByRef dtkesehatansiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "datakesehatan", "nis", VarNIS}
        If Utils.executeSP("proc_searchallEqual", strQuery, dtkesehatansiswa) Then
            dtkesehatansiswa = dtkesehatansiswa
        Else
            dtkesehatansiswa = Nothing
        End If
    End Sub

    Private Sub RefreshGrid()
        Dim dtsiswa As New DataTable
        LoadDataSiswa(dtsiswa)
        Dim item As New RepositoryItemPictureEdit()
        item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
        GridControl1.RepositoryItems.Add(item)
        If dtsiswa.Rows.Count > 0 Then
            GridView1.Columns("foto").ColumnEdit = item
        End If
        GridView1.OptionsBehavior.Editable = False
        GridView1.OptionsView.ShowGroupPanel = False
    End Sub
    Private Sub DialogCariSiswa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshGrid()
        cbTipeCari.SelectedIndex = 0
        VarNIS = ""
    End Sub

    Private Sub DialogCariSiswa_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If Not DialogResult = Windows.Forms.DialogResult.OK Then
            DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub
End Class